package com.hikvh.media;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.hikvh.media.amr.AmrEncoder;
import com.hikvh.media.sound.SoundMan;

public class MainActivity extends  Activity {

	Button recordButton;
    boolean isRecording;
	private String TAG="MainActivity";
	 final private static byte[] header = new byte[]{0x23, 0x21, 0x41, 0x4D, 0x52, 0x0A};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        KCacheUtils.init(this);

        recordButton = (Button) findViewById(R.id.recordButton);
        recordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                triggerButton();
            }
        });

		
       
    }

    private void triggerButton() {
        if (isRecording) {
            SoundMan.getInstance().stop();
            recordButton.setText(R.string.record_start);
        } else {
            SoundMan.getInstance().start();
            recordButton.setText(R.string.record_stop);

            Toast.makeText(this, String.format("Check your file at: %s", KCacheUtils.getCacheDirectory() + "/record"), Toast.LENGTH_LONG).show();
        }

        isRecording = !isRecording;
    }
    
    /**
     * 通过反射调用android系统自身AmrInputStream类进行转换
     * @param inPath 源文件
     * @param outPath 目标文件
     */
    public void systemWav2Amr(String inPath,String outPath){
		try {
			FileInputStream fileInputStream = new FileInputStream(inPath);
			FileOutputStream fileoutputStream = new FileOutputStream(outPath);
			// 获得Class
			Class<?> cls = Class.forName("android.media.AmrInputStream");
			// 通过Class获得所对应对象的方法
			Method[] methods = cls.getMethods();
			// 输出每个方法名
			fileoutputStream.write(header);
			Constructor<?> con = cls.getConstructor(InputStream.class);
			Object obj = con.newInstance(fileInputStream);
			for (Method method : methods) {
				Class<?>[] parameterTypes = method.getParameterTypes();
				if ("read".equals(method.getName())
						&& parameterTypes.length == 3) {
					byte[] buf = new byte[1024];
					int len = 0;
					while ((len = (int) method.invoke(obj, buf, 0, 1024)) > 0) {
						fileoutputStream.write(buf, 0, len);
					}
					break;
				}
			}
			for (Method method : methods) {
				if ("close".equals(method.getName())) {
					method.invoke(obj);
					break;
				}
			}
			fileoutputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    private void wav2amr(final String inpath,final String outpath){
    	//	Random random = new Random();
//		File file = new File(root + "/RawAudio.raw");
//    	file.getAbsolutePath(),root + "/test" + random.nextInt(120) + ".amr"
    	new Thread(new Runnable() {
			@Override
			public void run() {
				convertAMR(inpath,outpath);
			}
		}).start();
    }
    /**
     * 将wav或raw文件转换成amr
     * @param inpath 源文件
     * @param outpath 目标文件
     */
    private void convertAMR(String inpath,String outpath){
    	try {
    		AmrEncoder.init(0);
    		File inFile = new File(inpath);
    		List<short[]> armsList = new ArrayList<short[]>();
			FileInputStream inputStream = new FileInputStream(inFile);
			FileOutputStream outStream = new FileOutputStream(outpath);
			//写入Amr头文件
			outStream.write(header);

			int byteSize = 320;
    		byte[] buff = new byte[byteSize];
			int rc = 0;
			while ((rc = inputStream.read(buff, 0, byteSize)) > 0) {
				short[] shortTemp = new short[160];
				//将byte[]转换成short[]
				ByteBuffer.wrap(buff).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shortTemp);  
				//将short[]转换成byte[] 
//				ByteBuffer.wrap(buff).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(shortTemp);  
				armsList.add(shortTemp);
			}
			 
			for (int i = 0; i < armsList.size(); i++) {
				int size = armsList.get(i).length;
				byte[] encodedData = new byte[size*2];
				int len = AmrEncoder.encode(AmrEncoder.Mode.MR122.ordinal(), armsList.get(i), encodedData);
				if (len>0) {
					byte[] tempBuf = new byte[len];
					System.arraycopy(encodedData, 0, tempBuf, 0, len);
					outStream.write(tempBuf, 0, len);
				}
			}
			AmrEncoder.reset();
			AmrEncoder.exit();
			
			outStream.close();
			inputStream.close();
			System.out.println("convert success ... "+outpath);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    private void moveFile(File oldFile, File newFile) throws IOException {  
        if (!oldFile.exists()) {  
            throw new IOException("oldFile is not exists. in moveFile() fun");  
        }  
        if (oldFile.length() <= 0) {  
            throw new IOException("oldFile size = 0. in moveFile() fun");  
        }  
        BufferedInputStream reader = new BufferedInputStream(new FileInputStream(oldFile));  
        BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(newFile,  
                false));  

        final byte[] AMR_HEAD = new byte[] { 0x23, 0x21, 0x41, 0x4D, 0x52, 0x0A };  
        writer.write(AMR_HEAD, 0, AMR_HEAD.length);  
        writer.flush();  

        try {  
            byte[] buffer = new byte[1024];  
            int numOfRead = 0;  
            Log.d(TAG, "POS...newFile.length=" + newFile.length() + "  old=" + oldFile.length());  
            while ((numOfRead = reader.read(buffer, 0, buffer.length)) != -1) {  
                writer.write(buffer, 0, numOfRead);  
                writer.flush();  
            }  
            Log.d(TAG, "POS..AFTER...newFile.length=" + newFile.length());  
        } catch (IOException e) {  
            Log.e(TAG, "moveFile error.. in moveFile() fun." + e.getMessage());  
            throw new IOException("moveFile error.. in moveFile() fun.");  
        } finally {  
            if (reader != null) {  
                reader.close();  
                reader = null;  
            }  
            if (writer != null) {  
                writer.close();  
                writer = null;  
            }  
        }  
    }  
     
}
